package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();

        return new PageResult(total, result);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        //先向套餐表中插入数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        //再向套餐菜品关系表插入数据
        Long setmealId = setmeal.getId();   //获取套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId)); //绑定套餐id
        setmealDishMapper.insertBatch(setmealDishes);   //保存套餐和菜品的关联关系


    }

    /**
     * 修改套餐及关联的菜品
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void updateWithDishes(SetmealDTO setmealDTO) {
        //先修改套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //再把套餐菜品关联表的数据直接删除
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealIds(Collections.singletonList(setmealId));

        //给套餐菜品表数据的套餐id赋值(前端不传递套餐id)
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        //最后重新添加套餐菜品表
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        //先获取套餐基本信息
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        //再获取套餐关联菜品信息
        List<SetmealDish> setmealDish = setmealDishMapper.getSetmealDishBySetmealId(id);
        //将属性封装到SetmealVO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDish);

        return setmealVO;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //先删除套餐菜品关联信息
        setmealDishMapper.deleteBySetmealIds(ids);

        //再删除套餐信息
        setmealMapper.deleteByIds(ids);

    }

    /**
     * 套餐起售、停售
     *
     * @param id
     * @param status
     * @return
     */
    public void startOrStop(Long id, Integer status) {
        //如果是启用操作, 判断套餐中菜品是否有禁售菜品
        if (status.equals(StatusConstant.ENABLE)) {
            //获取套餐中的菜品
            List<SetmealDish> setmealDishBySetmealId = setmealDishMapper.getSetmealDishBySetmealId(id);
            setmealDishBySetmealId.forEach(setmealDish -> {
                //获取菜品基本信息
                Dish dish = dishMapper.getById(setmealDish.getDishId());
                //判断菜品是否禁售中
                if (dish.getStatus().equals(StatusConstant.DISABLE)){
                    //抛出异常
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        //对套餐进行起售禁售操作
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @param setmealId
     * @return
     */
    public List<DishItemVO> getDishItemById(Long setmealId) {
        //获取套餐菜品关联信息
        List<SetmealDish> setmealDishList = setmealDishMapper.getSetmealDishBySetmealId(setmealId);
        //创建一个List, 用于存储返回数据
        List<DishItemVO> dishItemVOList = new ArrayList<>();

        setmealDishList.forEach(setmealDish -> {
            //创建一个dishItemVO对象, 用于存储中间数据
            DishItemVO dishItemVO = new DishItemVO();
            //根据菜品id获取菜品信息
            Dish dish = dishMapper.getById(setmealDish.getDishId());
            //数据拷贝
            BeanUtils.copyProperties(dish, dishItemVO);
            //给copies属性赋值
            dishItemVO.setCopies(setmealDish.getCopies());
            //将数据添加到List
            dishItemVOList.add(dishItemVO);
        });

        return dishItemVOList;
    }
}
